﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.IO;
using System.Security.AccessControl;
using System.Text;

namespace Client
{
    /// <summary>
    /// 计算
    /// </summary>
    public class CalcHelper
    {

        public void Calc(IList<CalcItem> list, double paramA, double paramN, double paramM)
        {
            sortList(list);
            double normalPrize = calcNormalPrize(list) * (1 - paramA);
            foreach (var item in list)
            {
                if (item.Prize >= normalPrize)
                {
                    item.Ponit = Math.Pow(normalPrize / item.Prize, paramN) * 100;
                }
                else
                {
                    item.Ponit = Math.Pow(item.Prize / normalPrize, paramM) * 100;
                }
            }
        }

        private double calcNormalPrize(IList<CalcItem> list)
        {
            double sum = 0;
            int count = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list.Count < 6)
                {
                    sum += list[i].Prize;
                    count++;
                }
                else if (list.Count >= 6 && list.Count < 10)
                {
                    if (i == 0 || i == list.Count - 1)
                    {
                        continue;
                    }
                    sum += list[i].Prize;
                    count++;
                }
                else
                {
                    if (i < 2 || i == list.Count - 1)
                    {
                        continue;
                    }
                    sum += list[i].Prize;
                    count++;
                }
            }
            if (count != 0)
            {
                return sum / count;
            }
            return -1;
        }

        /// <summary>
        /// 按照降序排列
        /// </summary>
        /// <param name="source">需要排序的列表</param>
        private static void sortList(IList<CalcItem> source)
        {
            for (int i = 1; i < source.Count; i++)
            {
                CalcItem temp = source[i];
                double prize = temp.Prize;
                int j = i;
                while (j > 0 && source[j - 1].Prize < prize)
                {
                    source[j] = source[j - 1];
                    --j;
                }
                source[j] = temp;
            }
        }


        internal IList<CalcItem> GetCalcItemList(string fileName)
        {
            var list = new List<CalcItem>();
            CalcItem item = null;
            using (var reader = new StreamReader(fileName, Encoding.GetEncoding("GBK")))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (!string.IsNullOrEmpty(line))
                    {
                        string[] array = line.Split(",".ToCharArray());
                        if (array.Length >= 2)
                        {
                            item = new CalcItem();
                            item.Name = array[0];
                            item.Prize = Convert.ToDouble(array[1]);
                            list.Add(item);
                        }
                    }
                }
            }
            return list;
        }
    }

    public class CalcItem
    {
        /// <summary>
        /// 加个
        /// </summary>
        public double Prize { get; set; }

        /// <summary>
        /// 分值
        /// </summary>
        public double Ponit { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
    }
}
